from . import mon_bp
from flask import jsonify,make_response,request,current_app
from flask_restful import Api,Resource

from flask import current_app as app
from flask import render_template

from lib import response

from model import user
from werkzeug.security import check_password_hash
from authlib.jose import jwt, JoseError

import time

class login():
    def post(self):
        self.username = request.json.get("username")
        self.password = request.json.get("password")

        tmp_user = user.query.filter_by(username=self.username.data).first()
        if tmp_user and check_password_hash(user.password, self.password.data):
            # if user :
            #     print("test2")
            return user
        # return response(message='登陆成功', data={"token": token, "username": user.username})

def create_token(uid):
    #第一个参数  传递内部私钥
    # 第二个参数表示过期时间
    # s = TJS(current_app.config['SECRET_KEY'], expires_in = current_app.config['EXPIRES'])
    # token = s.dumps({"uid":uid}).decode('ascii')

    payload = {
        "user_id": uid,
        "exp": int(time.time()) + current_app.config['EXPIRES']  # 1小时后过期
    }
    # 生成 JWT
    token = jwt.encode(
        {"alg": "HS256"},  # 使用 HS256 算法
        payload,
        current_app.config['SECRET_KEY']
    ).decode('utf-8')
    return token




api = Api(mon_bp)
api.add_resource(login,'/login')